#############################################################################################################################
#### Uncertainty, Cleavages and Ethnic Coalitions
### Duration Analysis
### Nils-Christian Bormann
### Journal of Politics

####################################################################################################
### Header
rm(list=ls())
setwd("") # set working directory

# load libraries
# install.packages("", dependencies=T)
library(survival); library(multcomp); library(stargazer)

####################################################################################################
### Data
dat <- read.csv("ReplicationData-EthnicCoalitions-Duration-Bormann-180209.csv", header=T, sep=",")
names(dat)

## Outcome Variable:
# create survival object for failure
fail_surv <- with(dat, Surv(time=govyearstart, time2=govyearstop, event=failure))

## Explanatory Variables:
# singlemaj_l1 - single majority rule, binary, lagged
# coalmaj_l1 - oversized coalition, binary, lagged
# coal_maxgrp_flag_l1 - largest groups in country in government, binary, lagged
# membercount_l1 - number of groups represented in government, count, lagged
# govcleav_relctr - share of cleavages in government out of cleavages in country 

## Control Variables:
# incidence_gov_flag - ongoing governmental armed conflict (UCDP; 25 annual battle-deaths), binary
# exit_irr_flag - irregular leader exit (Archigos), binary
# ln_tenure - length of leader tenure in years (Archigos), count, logged
# ln_gdp_l1 - 2005 PPP real GDP per capita, logged, lagged (Hunziker & Bormann 2013)
# growth_l1 - growth in GDP per capita, lagged (Hunziker & Bormann 2013)
# ln_pop_l1 - population size, logged, lagged (Hunziker & Bormann 2013)
# dd_parl_l1 - parliamentary democratic regime, binary, lagged (Cheibub et al. 2010 Democracy & Dictatorship data)
# dd_spre_l1 - semi-presidential democratic regime, see above
# dd_pres_l1 - presidential democratic regime, see above
# dd_civi_l1 - civilian dictatorship, see above
# dd_mili_l1 - military dictatorship, see above
# pr_l1 - proportional representation electoral system in democracy, binary, lagged (Bormann & Golder 2013)
# maj_l1 - majoritarian electoral system in democracy, see above
# mix_l1 - mixed electoral system in democracy, see above
# dd_oneparty_l1 - single-party dictatorship, binary, lagged (Cheibub et al. 2010 Democracy & Dictatorship data)
# dd_mparty_l1 - multi-party dictatorship, see above


####################################################################################################
#### Main paper
### Table 2 & Table A21 in Online Appendix

## Model 6: main explanatory variables
cm1a <-  coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
                 coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr +
                 cluster(cowid), data=dat)
summary(cm1a); BIC(cm1a)
summary(glht(cm1a, linfct = c("coalmaj_l1 - singlemaj_l1 = 0"))) # two-sided
cox.zph(cm1a) # proportional hazards assumption not violated

## Model 7: main explanatory variables + violence controls
cm2a <-  coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
                 coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 + cluster(cowid), data=dat)
summary(cm2a); BIC(cm2a)
summary(glht(cm2a, linfct = c("coalmaj_l1 - singlemaj_l1 = 0"))) # two-sided
cox.zph(cm2a) # proportional hazards assumption not violated

## Model 8: main explanatory variables + violence & socioeconomic controls
cm3a <-  coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
                 coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                 + cluster(cowid), data=dat)
summary(cm3a); BIC(cm3a)
summary(glht(cm3a, linfct = c("coalmaj_l1 - singlemaj_l1 = 0"))) # two-sided
cox.zph(cm3a) # proportional hazards assumption not violated

## Model 9: main explanatory variables + violence, socioeconomic & regime controls
cm4a <-  coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
                 coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                 dd_parl_l1 + dd_spre_l1 + dd_pres_l1 + dd_civi_l1 + dd_mili_l1 + 
                 + cluster(cowid), data=dat)
summary(cm4a); BIC(cm4a)
summary(glht(cm4a, linfct = c("coalmaj_l1 - singlemaj_l1 = 0"))) # two-sided
cox.zph(cm4a) # proportional hazards assumption not violated

## Model 10: main explanatory variables + violence, socioeconomic & electoral system controls
cm5a <-  coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
                 coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 ln_gdp_l1 + growth_l1 + ln_pop_l1 + 
                 pr_l1 + maj_l1 + mix_l1 + dd_oneparty_l1 + dd_mparty_l1 +
                 + cluster(cowid), data=dat)
summary(cm5a); BIC(cm5a)
summary(glht(cm5a, linfct = c("coalmaj_l1 - singlemaj_l1 = 0"))) # two-sided
cox.zph(cm5a) # proportional hazards assumption not violated


####################################################################################################
#### Online Appendix
### Table A8 - Summary Statistics

## Summary Statistics of Main variables
stargazer(dat[,c("failure", "singlemaj_l1", "coalmaj_l1", "coal_maxgrp_flag_l1",
                 "membercount_l1", "govcleav_relctr", #"govcleav_count", 
                 "incidence_gov_flag",  "exit_irr_flag", "ln_tenure",
						     "ln_gdp_l1", "growth_l1", "ln_pop_l1",
						     "dd_parl_l1", "dd_spre_l1", "dd_pres_l1", 
						     "dd_civi_l1", "dd_mili_l1", "pr_l1", "maj_l1", 
						     "mix_l1", "dd_oneparty_l1", "dd_mparty_l1")]
		, covariate.labels=c("Government Failure", 
				"Single-Group Majority", "Oversized Coalition", "Largest Group",
				"Member Count", "Cleavage Dimensions", 
				"Ethnic Gov. Conflict", "Irregular Leader Change", "Log(Leader Tenure)",
		    "Log(GDP p.c.)", "GDP Growth", "Log(Population)", 
		    "Parliamentary (DD)", "Semi-Presidential (DD)",  
		    "Presidential (DD)", "Civilian (DD)", "Military (DD)", 
		    "PR (BG)", "Majoritarian (BG)", "Mixed (BG)", 
		    "One-Party (Gandhi)", "Multi-Party (Gandhi)"),
		title="Summary Statistics of Main Duration Analysis Variables.",
		label="tab:dursum"
)


######################################
### Robustness Checks Main Model
## Table A22: Democratic & Autocratic Institutions
# Model A50: base model + polity
cm6 <- coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
               coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
               incidence_gov_flag + exit_irr_flag + ln_tenure +
               ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
               polity_l1 +
               cluster(cowid), data=dat)
summary(cm6); BIC(cm6)
summary(glht(cm6, linfct = c("coalmaj_l1-singlemaj_l1=0"))) # two-sided

# Model A51: base model + dd democracy and legislature institutions
cm7 <- coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
               coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
               incidence_gov_flag + exit_irr_flag + ln_tenure +
               ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
               dd_democracy_flag_l1 + dd_eleg_l1 + 
               cluster(cowid), data=dat)
summary(cm7); BIC(cm7)
summary(glht(cm7, linfct = c("coalmaj_l1-singlemaj_l1=0"))) # two-sided


# Model A52: base model + gwf institutions
cm8 <- coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
                coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
                incidence_gov_flag + exit_irr_flag + ln_tenure +
                ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                gwf_party_l1 + gwf_personal_l1 + gwf_military_l1 +
                cluster(cowid), data=dat)
summary(cm8); BIC(cm8)
summary(glht(cm8, linfct = c("coalmaj_l1-singlemaj_l1=0"))) # two-sided


## Table A23: Institutional Strength
# Model A53: base model + communist rebellion (Kalyvas & Balcells)
cm8 <- coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
               coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
               incidence_gov_flag + exit_irr_flag + ln_tenure +
               ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
               comreb_ongoing + comreb_past + 
               cluster(cowid), data=dat)
summary(cm8); BIC(cm8)
summary(glht(cm8, linfct = c("coalmaj_l1 - singlemaj_l1 = 0"))) # two-sided

# Model A54:  base model + party institutionalization (V-Dem)
cm9 <- coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
               coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
                incidence_gov_flag + exit_irr_flag + ln_tenure +
                ln_gdp_l1 + growth_l1 + ln_pop_l1 + 
                party_inst +
                cluster(cowid), data=dat)
summary(cm9); BIC(cm9)
summary(glht(cm9, linfct = c("coalmaj_l1 - singlemaj_l1 = 0"))) # two-sided

# Model A55: base model + state history variable
cm10 <- coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
                coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
                incidence_gov_flag + exit_irr_flag + ln_tenure +
                ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                stahis + cluster(cowid), data=dat)
summary(cm10); BIC(cm10)
summary(glht(cm10, linfct = c("coalmaj_l1 - singlemaj_l1 = 0"))) # two-sided

# Model A56: base model + settler mortality
cm11 <- coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
                coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
                incidence_gov_flag + exit_irr_flag + ln_tenure +
                ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                settmort + cluster(cowid), data=dat)
summary(cm11); BIC(cm11)
summary(glht(cm11, linfct = c("coalmaj_l1 - singlemaj_l1 = 0"))) # two-sided


## Table A24: Democracy and and democratic change
# Model A57: Polity foreign occupation
cm12 <-  coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
                 coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                 polity_occ_l1 + 
                 cluster(cowid), data=dat)
summary(cm12); BIC(cm12)
summary(glht(cm12, linfct = c("coalmaj_l1 - singlemaj_l1 = 0"))) # two-sided


# Model A58: controlling for Polity change
cm13 <-  coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
                 coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                 dpolity_l1 +
                 cluster(cowid), data=dat)
summary(cm13); BIC(cm13)
summary(glht(cm13, linfct = c("coalmaj_l1 - singlemaj_l1 = 0"))) # two-sided

# Model A59: controlling for history of conflict
cm14 <-  coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
                 coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                 warhist +
                 cluster(cowid), data=dat)
summary(cm14); BIC(cm14)
summary(glht(cm14, linfct = c("coalmaj_l1 - singlemaj_l1 = 0"))) # two-sided

# Model A60:  controlling for DD transitions
cm15 <-  coxph(fail_surv ~ singlemaj_l1 + coalmaj_l1 + 
                 coal_maxgrp_flag_l1 + membercount_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                 dd_demtrans + dd_auttrans +
                 cluster(cowid), data=dat)
summary(cm15); BIC(cm15)
summary(glht(cm15, linfct = c("coalmaj_l1 - singlemaj_l1 = 0"))) # two-sided


######################################
### Robustness Checks Continuous Coalition Variables Model
## Table A25: Main model 
# Model A63 continuous base model
cm1b <-  coxph(fail_surv ~ rmcount_l1 + size_l1 + 
                 coal_maxgrp_flag_l1 + govcleav_relctr +   
                 cluster(cowid), data=dat)
summary(cm1b); BIC(cm1b)
cox.zph(cm1b) # proportional hazards assumption not violated

## Model A64: continuous base model + violence controls
cm2b <-  coxph(fail_surv ~ rmcount_l1 + size_l1 + 
                 coal_maxgrp_flag_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 cluster(cowid), data=dat)
summary(cm2b); BIC(cm2b)
cox.zph(cm2b) # proportional hazards assumption not violated

## Model A65: continuous base model + violence & socio-economic controls
cm3b <-  coxph(fail_surv ~ rmcount_l1 + size_l1 + 
                 coal_maxgrp_flag_l1 + govcleav_relctr +  
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                 + cluster(cowid), data=dat)
summary(cm3b); BIC(cm3b)
cox.zph(cm3b) # proportional hazards assumption not violated

## Model A66: continuous base model + violence, socio-economic & regime controls 
cm4b <-  coxph(fail_surv ~ rmcount_l1 + size_l1 + 
                 coal_maxgrp_flag_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                 dd_parl_l1 + dd_spre_l1 + dd_pres_l1 + dd_civi_l1 + dd_mili_l1 + 
                 + cluster(cowid), data=dat)
summary(cm4b); BIC(cm4b)
cox.zph(cm4b) # proportional hazards assumption not violated

## Model A67: continuous base model + violence, socio-economic & electoral systems controls 
cm5b <-  coxph(fail_surv ~ rmcount_l1 + size_l1 + 
                 coal_maxgrp_flag_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 ln_gdp_l1 + growth_l1 + ln_pop_l1 + 
                 pr_l1 + maj_l1 + mix_l1 + dd_oneparty_l1 + dd_mparty_l1 +
                 + cluster(cowid), data=dat)
summary(cm5b); BIC(cm5b)
cox.zph(cm5b) # proportional hazards assumption not violated


## Table A26 : Democratic & Autocratic Institutions
# Model A68: continuous model + dd democratic institutions
cm16 <- coxph(fail_surv ~ rmcount_l1 + size_l1 + 
               coal_maxgrp_flag_l1 + govcleav_relctr + 
               incidence_gov_flag + exit_irr_flag + ln_tenure +
               ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
               polity_l1 + 
               cluster(cowid), data=dat)
summary(cm16); BIC(cm16)

# Model A69: base model + dd autocratic institutions
cm17 <- coxph(fail_surv ~ rmcount_l1 + size_l1 + 
               coal_maxgrp_flag_l1 + govcleav_relctr + 
               incidence_gov_flag + exit_irr_flag + ln_tenure +
               ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
               dd_democracy_flag_l1 + dd_eleg_l1 +
               cluster(cowid), data=dat)
summary(cm17); BIC(cm17)

# Model A70: base model + gwf institutions
cm18 <- coxph(fail_surv ~ rmcount_l1 + size_l1 + 
               coal_maxgrp_flag_l1 + govcleav_relctr + 
               incidence_gov_flag + exit_irr_flag + ln_tenure +
               ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
               gwf_party_l1 + gwf_personal_l1 + gwf_military_l1 +
               cluster(cowid), data=dat)
summary(cm18); BIC(cm18)


## Table A27: Continuous Variables & Institutional Strength 
# Model A71: continuous variable model + communist rebellion (Kalyvas & Balcells)
cm21 <- coxph(fail_surv ~ rmcount_l1 + size_l1 + 
               coal_maxgrp_flag_l1 + govcleav_relctr + 
               incidence_gov_flag + exit_irr_flag + ln_tenure +
               ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
               comreb_ongoing + comreb_past + 
               cluster(cowid), data=dat)
summary(cm21); BIC(cm21)

# Model A72: continuous variable model + party institutionalization (V-Dem)
cm22 <- coxph(fail_surv ~ rmcount_l1 + size_l1 + 
               coal_maxgrp_flag_l1 + govcleav_relctr + 
               incidence_gov_flag + exit_irr_flag + ln_tenure +
               ln_gdp_l1 + growth_l1 + ln_pop_l1 + 
               party_inst +
               cluster(cowid), data=dat)
summary(cm22); BIC(cm22)

# Model A73: continuous variable model + state history variable
cm23 <- coxph(fail_surv ~ rmcount_l1 + size_l1 + 
                coal_maxgrp_flag_l1 + govcleav_relctr + 
                incidence_gov_flag + exit_irr_flag + ln_tenure +
                ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                stahis + cluster(cowid), data=dat)
summary(cm23); BIC(cm23)

# Model A74: continuous variable model + settler mortality
cm24 <- coxph(fail_surv ~ rmcount_l1 + size_l1 + 
                coal_maxgrp_flag_l1 + govcleav_relctr + 
                incidence_gov_flag + exit_irr_flag + ln_tenure +
                ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                settmort + cluster(cowid), data=dat)
summary(cm24); BIC(cm24)


## Table A28: Democracy and and democratic change
# Model A75: continuous variable model + Polity level and foreign occupation
cm25 <-  coxph(fail_surv ~ rmcount_l1 + size_l1 + 
                 coal_maxgrp_flag_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                 polity_occ_l1 + 
                 cluster(cowid), data=dat)
summary(cm25); BIC(cm25)

# Model A76: continuous variable model + Polity change
cm26 <-  coxph(fail_surv ~ rmcount_l1 + size_l1 + 
                 coal_maxgrp_flag_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                 dpolity_l1 +
                 cluster(cowid), data=dat)
summary(cm26); BIC(cm26)

# Model A77: continuous variable model + DD democracy
cm27 <-  coxph(fail_surv ~ rmcount_l1 + size_l1 + 
                 coal_maxgrp_flag_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                 warhist +
                 cluster(cowid), data=dat)
summary(cm27); BIC(cm27)

# Model A78: continuous variable model + DD transitions
cm28 <-  coxph(fail_surv ~ rmcount_l1 + size_l1 + 
                 coal_maxgrp_flag_l1 + govcleav_relctr + 
                 incidence_gov_flag + exit_irr_flag + ln_tenure +
                 ln_gdp_l1 + growth_l1 + ln_pop_l1 +  
                 dd_demtrans + dd_auttrans +
                 cluster(cowid), data=dat)
summary(cm28); BIC(cm28)


